package com.alex.common.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;

/**
 * usage:
 * 
 * <pre>
 * 	<filter>
 * 		<filter-name>CharacterEncodingFilter</filter-name>
 * 		<filter-class>
 * 			com.alex.common.filter.CharacterEncodingFilter
 * 		</filter-class>
 * 		<init-param>
 * 			<param-name>encoding</param-name>
 * 			<param-value>UTF-8</param-value>
 * 		</init-param>
 * 	</filter>
 * 
 * 	<filter-mapping>
 * 		<filter-name>CharacterEncodingFilter</filter-name>
 * 		<url-pattern>/*</url-pattern>
 * 	</filter-mapping>
 * 
 * </pre>
 * 
 * @author Alex hu
 * 
 */
public class CharacterEncodingFilter implements Filter {

	private String encoding;

	public void destroy() {
	}

	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {
		((HttpServletRequest) request).setCharacterEncoding(encoding);
		chain.doFilter(request, response);
	}

	public void init(FilterConfig config) throws ServletException {
		this.encoding = config.getInitParameter("encoding");
	}
}
